﻿
using System.Text;
namespace SRS
{
    public class Helper
    {
        public static byte[] StringToArrayByte(string s)
        {
            int length = s.Length;
            byte[] bytes = new byte[length];

            for (int i = 0; i < length; ++i)
            {
                bytes[i] = (byte)s[i];
            }

            return bytes;
        }

        public static string ArrayByteToString(byte[] bytes)
        {
            int length = bytes.Length;
            StringBuilder sb = new StringBuilder(length);
            char[] chars;

            // To chars
            chars = ArrayByteToArrayChar(bytes);

            // copy to string builder
            sb.Append(chars);

            return sb.ToString();
        }

        private static char[] ArrayByteToArrayChar(byte[] bytes)
        {
            int length = bytes.Length;
            char[] chars = new char[length];

            for (int i = 0; i < length; ++i)
            {
                chars[i] = (char)bytes[i];
            }

            return chars;
        }

        /// <summary>
        /// Concat array
        /// </summary>
        /// <param name="arr1">First array</param>
        /// <param name="arr2">Second array</param>
        /// <returns></returns>
        public static byte[] Concat(byte[] arr1, byte[] arr2)
        {
            int arr1Length = arr1.Length;
            int arr2Length = arr2.Length;
            int length = arr1Length + arr2Length;
            byte[] concat = new byte[length];

            for (int i = 0; i < arr1Length; ++i)
            {
                concat[i] = arr1[i];
            }

            for (int i = arr1Length; i < length; ++i)
            {
                concat[i] = arr2[i - arr1Length];
            }

            return concat;
        }


        /// <summary>
        /// Convert to ASCII
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] ToASCII(byte[] bytes)
        {
            int length = bytes.Length;
            byte[] ascii = new byte[length];

            for (int i = 0; i < length; ++i)
            {
                ascii[i] = (byte)(bytes[i] % 128);
            }

            return ascii;
        }
    }
}
